<?php
// 本类由系统自动生成，仅供测试用途
class BulletinAction extends CommonAction{
	 
	public function index() {
		import("ORG.Util.Page");
		$b = d('bulletin');
		$count = $b->count();
		$p  = new Page ($count, 5 );
		$b_arr = $b->order('time desc')->limit($p->firstRow.','.$p->listRows)->findAll();		
		$page = $p->show(); 
		$this->assign ( "page", $page );
		$this->assign('b_arr',$b_arr);
		$this->display();
	}
	public function bull_admin(){
		$this->userChk();
		import("ORG.Util.Page");
		$b = d('bulletin');
		$count = $b->count();
		$p  = new Page ($count, 5 );
		$b_arr = $b->order('seq desc, time desc')->limit($p->firstRow.','.$p->listRows)->findAll();
				
		$page = $p->show(); 
		$this->assign ( "page", $page );
		$this->assign('b_arr',$b_arr);
		$this->display("Bulletin:bull_admin");
	}
	
	public function bull_add(){
		$this->userChk();
		
		if($_POST['bulletin_bt']){
			$b = d('Bulletin');
			if($b->create()){
				$b->content = $_POST['content'];
				
				if(false !==$b->add()) { 
					$this->assign('jumpUrl',"__URL__/bull_admin"); 
					$this->success('数据添加成功！'); 
				}
				else{
					
					$this->assign('jumpUrl',"__URL__/bull_add"); 
					$this->error('数据添加失败！'); 
				}
			}
			
		}
		$this->display("Bulletin:bull_add");
	}
	
	public function bull_edit(){
		$this->userChk();
		
		if($_GET['bid']){
			
			if($_POST['bulletin_bt']){
				$b = d('Bulletin');
				if($b->create()){
					$b->content = $_POST['content'];
					$b->id = $_GET['bid'];
					if(false !==$b->save()) { 
						$this->assign('jumpUrl',"__URL__/bull_admin"); 
						$this->success('公告修改成功！'); 
					}
					else{
						$this->assign('jumpUrl',"__URL__/bull_admin"); 
						$this->error('公告修改失败！'); 
					}
				}
			}
			else{
				$b = d('Bulletin')->where(array('id'=>$_GET['bid']))->find();
					
				$this->assign('bull',$b);
				$this->display("Bulletin:bull_add");
			}
		}
		
	}
	public function bull_del(){
		$this->userChk();
		
		if($_POST['row']){
			$bid_str = implode("','",$_POST['row']);
			if(d('bulletin')->where("id in ('".$bid_str."')")->delete()){
				$msg = "删除公告成功!";
			}
			else{
				$msg = "删除公告失败!";
			}
		}
		else{
			$msg = "请选择公告!";
		}
		$this->assign('jumpUrl',"__URL__/bull_admin"); 
		$this->success($msg);
	}
	
	public function bull_show(){
		
		if($_GET['bid']){
			$b = d('Bulletin')->where(array('id'=>$_GET['bid']))->find();
			$this->assign('bull',$b);
			$this->display("Bulletin:bull_show");
		}
		
	}
	public function userChk(){
		if(!Cookie::get('acc_name')){
				$this->assign('jumpUrl',"__APP__/Public/login"); 
				$this->error('请登录！');
				return false;
		}
		else{
			$name = Cookie::get('acc_name');
			$user = d('guild_member')->where(array('acc_name'=>$name))->find();
			if($user['glevel'] != "会长"){
				$this->assign('jumpUrl',"__APP__"); 
				$this->error('没有权限！');
				return false;
			}
		}
	}
	
	public function getBulletin($b_id="",$len='10'){
		$b = d('Bulletin');
		$result = "";
		if($b_id){
			
			$result = $b->where(array('id'=>$b_id))->find();
		}
		else{
			$result = $b->order('time desc')->limit($len)->findall();
		}
		return $result;
	}
}
?>